#pragma once

#include<iostream>
#include<assert.h>
using namespace std;

namespace zy
{
	template<class T>
	class vector
	{
	public:

		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}
		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		bool empty()
		{
			return _start == _finish;
		}

		T& operator[](size_t pos)
		{
			assert(pos >= 0);
			assert(pos < size());

			return *(_start + pos);
		}

		const T& operator[](size_t pos) const
		{
			assert(pos >= 0);
			assert(pos < size());

			return *(_start + pos);
		}

		void resize(size_t n,const T& val = T())
		{
			if (n > capacity())
			{
				reserve(n);
				while (_finish < _endofstorage)
				{
					*_finish = val;
					finish++;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t len = size();

				T* tmp = new T[n];
				memcpy(tmp, _start, size() * sizeof(T));
				delete[] _start;

				_start = tmp;
				_finish = _start + len;
				_endofstorage = _start + n;
			}
		}

		void push_back(const T& val)
		{
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}

			*_finish = val;
			_finish++;
		}

		void pop_back()
		{
			assert(!empty());

			_finish--;
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			if (_finish == _endofstorage)
			{
				size_t len = size();
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (_finish >= pos)
			{
				*(_finish + 1) = *_finish;
				_finish--;
			}

			*pos = val;
			_finish++;
		}

		void erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator begin = pos + 1;
			while (pos < _finish)
			{
				*(begin - 1) = *begin;
				begin++;
			}
			_finish--;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

	template<class T>
	void print(const vector<T>& v)
	{
		//for (auto e : v)
		//{
		//	cout << e << " ";
		//}
		//cout << endl;

		//for (size_t i = 0; i < v.size(); i++)
		//{
		//	cout << v[i] << " ";
		//}
		//cout << endl;

		auto it = v.begin();
		//typename vector<T>::const_iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		print(v);
		v.push_back(5);
		print(v);
		v.push_back(6);
		print(v);
		v.push_back(7);
		print(v);
		v.push_back(8);
		print(v);
	}
}
